<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/2/22
 * Time: 14:25
 */

namespace App\Services\Job;


use App\Http\Controllers\BaseApiController;
use App\Model\Position;
use App\Services\CommonService;

class JobService extends CommonService
{
    private $Job;

    private $code;

    public function __construct()
    {
     $this->Job=new Position();
     $this->code=new BaseApiController();
    }

    /**
     * @param
     * @return array
     * 职位添加
     */
    public function addPosition($data){
        $high=$data['high'];
        $arr=explode("-",$high);
        $data['high']=$arr[1]/1000;
        $data['low']=$arr[0]/1000;
        ini_set('date.timezone','Asia/Shanghai');
        $time=date('Y-m-d H:i:s',time());
        $dee=$data['describe'];
        $dee=json_decode($dee);
        $describ=implode(",",$dee);
        $model=new Position();
        $model->position_name=$data['position_name'];
        $model->nature_work=$data['nature_work'];
        $model->address=$data['address'];
        $model->low=$data['low'];
        $model->high=$data['high'];
        $model->education=$data['education'];
        $model->work_life=$data['work_life'];
        $model->job_category=$data['job_category'];
        $model->industry_id=$data['industry_id'];
        $model->number_recruits=$data['number_recruits'];
        $model->company_id=$data['company_id'];
        $model->status=$data['status'];
        $model->describe=$describ;
        $model->create_time=$time;
        $res=$model->save();
        //判断返回值，是否添加成功;
        if($res){
            $mgs=$this->code->result('200','职位添加成功',$res);
        }else{
            $mgs=$this->code->result('1','职位添加失败',$res);
        }
        return $mgs;
    }
    /**
     * 查询某公司下发布的所有职位信息
     */

    public function showJob($id)
    {
        $res = $this->Job->orderByRaw('is_top desc')->where('company_id',$id)->get()->toArray();
        return $res;
    }
    /**
     * 删除信息
     */
    public function delJob($id){
        $res=$this->Job->delJob($id);
       if($res){
           $mgs=$this->code->result('200','职位删除成功',$res);
       }else{
           $mgs=$this->code->result('1','职位删除失败',$res);
       }
       return $mgs;
    }

    /**
     * 修改职位状态
     */
    public function statusShut($id,$status){
        $num="";
        $text="";
        //判断该职位的当前的状态，1开启，2关闭
        if($status==1){
            $num=2;
            $text="关闭";
        }
        if($status==2){
            $num=1;
            $text="开启";
        }
        $res=$this->Job->statusShut($id,$num);
        if($res){
            $mgs=$this->code->result('200','职位已'.$text,$res);
        }else{
            $mgs=$this->code->result('1','职位状态修改出错',$res);
        }
        return $mgs;
    }
    /**
     * 重命名
     */
    public function updName($data){
        $new_name=$data['new_name'];
        $position_id=$data['position_id'];
        $job=$this->Job->updName($position_id,$new_name);
        if($job){
            $mgs=$this->code->result('200','重命名成功',$job);
        }else{
            $mgs=$this->code->result('1','重命名失败',$job);
        }
    }
    /**
     * 置顶
     */
    public function Top($position_id,$company_id){
        $num=$this->Job->orderByRaw('is_top desc')->where('company_id',$company_id)->first()->toArray();
        $num=$num['is_top'];
        $num++;
        return $this->Job->where('position_id',$position_id)->update(['is_top'=>$num]);
    }
}